//
//  TNToolCabinetView.m
//  TravelNotes
//
//  Created by 黄锋 on 15/8/11.
//  Copyright (c) 2015年 王浩宇. All rights reserved.
//

#import "TNToolCabinetView.h"
#import "TNToolCabinetCollectionViewCell.h"
#import "Macro.h"
#import <CoreText/CoreText.h>

@interface TNToolCabinetView () <UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@property(nonatomic, strong) NSArray *titleArr;
@property(nonatomic, strong) NSArray *imageArr;

@end

@implementation TNToolCabinetView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        _titleArr = @[@"语音翻译", @"实时汇率", @"旅行记账", @"旅行地图"];
        _imageArr = @[@"3.0", @"3.1",@"3.2", @"3.3"];
        UIView *upperView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height / 5 * 2)];
        upperView.backgroundColor = [UIColor lightGrayColor];
        [self addSubview:upperView];
        _temp_min = [[UILabel alloc] initWithFrame:CGRectMake(100, 50, 50, 50)];
        _temp_min.font = [UIFont systemFontOfSize:30];
        _temp_min.textColor = [UIColor blueColor];
        [upperView addSubview:_temp_min];
        UILabel *lable0 = [[UILabel alloc] initWithFrame:CGRectMake(150, 50, 30, 20)];
        lable0.text = @"℃";
        lable0.font = [UIFont systemFontOfSize:15];
        lable0.textColor = [UIColor blueColor];
        [upperView addSubview:lable0];
        UILabel *lable1 = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH / 2 - 10, 50, 20, 50)];
        lable1.text = @"--";
        [upperView addSubview:lable1];
        _temp_max = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 150, 50, 50, 50)];
        _temp_max.font = [UIFont systemFontOfSize:30];
        _temp_max.textColor = [UIColor redColor];
        [upperView addSubview:_temp_max];
        UILabel *lable2 = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 100, 50, 30, 20)];
        lable2.text = @"℃";
        lable2.font = [UIFont systemFontOfSize:15];
        lable2.textColor = [UIColor redColor];
        [upperView addSubview:lable2];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
        [upperView addGestureRecognizer:tap];

        _timeLable = [[UILabel alloc] initWithFrame:CGRectMake(100, 120, frame.size.width - 200, 15)];
        _timeLable.textAlignment = NSTextAlignmentCenter;
        _timeLable.font = [UIFont systemFontOfSize:12];
        [upperView addSubview:_timeLable];
        
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.minimumInteritemSpacing = 0;
        flowLayout.minimumLineSpacing = 0;
        _toolCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, frame.size.height / 5 * 2, frame.size.width, frame.size.height / 5 * 3) collectionViewLayout:flowLayout];
        _toolCollectionView.backgroundColor = [UIColor whiteColor];
        _toolCollectionView.delegate = self;
        _toolCollectionView.dataSource = self;
        [_toolCollectionView registerClass:[TNToolCabinetCollectionViewCell class] forCellWithReuseIdentifier:@"TNToolCabinetCollectionViewCell"];
        [self addSubview:_toolCollectionView];
    }
    return self;
}

- (void)tapAction:(UITapGestureRecognizer *)sender
{
    if (_tapblock) {
        _tapblock();
    }
}

- (void)setModel:(TNCurrentModel *)model
{
    _model = model;
    _temp_min.text = [model.temp_min stringValue];
    _temp_max.text = [model.temp_max stringValue];
    _timeLable.text = [NSString stringWithFormat:@"当地时间 %@", model.current_time];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 4;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 5);
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    TNToolCabinetCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TNToolCabinetCollectionViewCell" forIndexPath:indexPath];
    cell.pic.image = [UIImage imageNamed:_imageArr[indexPath.item]];
    cell.lable.text = _titleArr[indexPath.item];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (_toolCabinetBlock) {
        _toolCabinetBlock(indexPath.item);
    }
}

@end
